/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiBuscarMotorista.java
 *
 * Created on 26/11/2011, 11:35:19
 */
package view.gui;

import java.util.List;
import javax.swing.table.DefaultTableModel;
import model.dao.DaoMotorista;
import model.dominio.Motorista;

/**
 *
 * @author Leandro
 */
public class GuiBuscarMotorista  extends JInternalFramePermissao {

    private int telaOrigem;
    private List<Motorista> lista;

    /** Creates new form GuiBuscarMotorista */
    public GuiBuscarMotorista(int tela) {
        initComponents();
        telaOrigem = tela;
    }

    public void buscar() {
        Motorista m = new Motorista();
        if (!boxBuscaCpf.getText().equals("   .   .   -  ")) {
            m.setCpf(boxBuscaCpf.getText());
        } else if (!boxBuscaNome.getText().isEmpty()) {
            m.setNome(boxBuscaNome.getText());
        }
        lista = new DaoMotorista().buscarMotorista(m);
        DefaultTableModel model = (DefaultTableModel) tabelaMotorista.getModel();

        model.setRowCount(0);
        String[] s = new String[4];
        for (Motorista motorista : lista) {
            s[0] = motorista.getNome();
            s[1] = motorista.getCpf();
            s[2] = motorista.getCnh();
            model.addRow(s);
        }
    }

    public void enviaMotorista(Motorista motorista) {
        switch (telaOrigem) {
            case 1:
                GuiPrincipal.addJanela(new GuiDadosMotorista(motorista));
                break;
            case 2:
                GuiDadosLocacao.addMotorista(motorista);
                break;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        boxBuscaNome = new javax.swing.JTextField();
        boxBuscaCpf = new javax.swing.JFormattedTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaMotorista = new javax.swing.JTable();

        setClosable(true);
        setTitle("Buscar Motorista");
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros de Busca"));
        jPanel1.setLayout(null);

        jLabel2.setText("CPF: ");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(340, 30, 40, 30);

        boxBuscaNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxBuscaNomeActionPerformed(evt);
            }
        });
        jPanel1.add(boxBuscaNome);
        boxBuscaNome.setBounds(56, 30, 270, 30);

        try {
            boxBuscaCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        boxBuscaCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxBuscaCpfActionPerformed(evt);
            }
        });
        jPanel1.add(boxBuscaCpf);
        boxBuscaCpf.setBounds(380, 30, 110, 30);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/Search-icon.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(510, 30, 50, 40);

        jLabel1.setText("Nome:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 30, 50, 30);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 580, 90);

        tabelaMotorista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "CPF", "CNH"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaMotorista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMotoristaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaMotorista);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(0, 90, 580, 190);

        setBounds(0, 0, 610, 322);
    }// </editor-fold>//GEN-END:initComponents

    private void boxBuscaNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxBuscaNomeActionPerformed
        buscar();
    }//GEN-LAST:event_boxBuscaNomeActionPerformed

    private void boxBuscaCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxBuscaCpfActionPerformed
        buscar();
    }//GEN-LAST:event_boxBuscaCpfActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        buscar();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tabelaMotoristaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMotoristaMouseClicked
        if (evt.getClickCount() > 1) {
            int linha = tabelaMotorista.getSelectedRow();
            Motorista motorista = lista.get(linha);
            enviaMotorista(motorista);
            this.dispose();
        }
    }//GEN-LAST:event_tabelaMotoristaMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField boxBuscaCpf;
    private javax.swing.JTextField boxBuscaNome;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaMotorista;
    // End of variables declaration//GEN-END:variables

    @Override
    public int getPermissao() {
        return 22;
    }
}
